<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * FlyTomorrow_model
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class FlyTomorrow_model extends MY_Model{
    public function __construct(){
            parent::__construct();
            $this->table_name = 'book_flytomorrow';
    }
    
    public function updateAllCus($params){
            $data = array(
                'phone_callstatus'  =>  $params['data']->phone_callstatus,
                'status'            =>  $params['data']->status,
                'note'              =>  $params['data']->note,
                'phone_calltime'    =>  $params['data']->phone_calltime
            );
            
            $this->db->where('customerID', $params['customerID']);
            $this->db->where('booking_place_code', $params['booking_place_code']);
            $this->db->where('airline', $params['airline']);
            
            $this->db->update($this->table_name, $data);
    }

    public function getAllShip($params){
            $this->db->select('booking,booking_types');
            $this->db->from('book_history');
            
            if($params['booking_place_code']){
                $this->db->where('booking_place_code',$params['booking_place_code']);
            }
            
            if($params['airline']){
                $this->db->where('airline',$params['airline']);
            }
            $this->db->where('booking_types',1);
            
            $this->db->order_by('createdTime','DESC');

            $query = $this->db->get();
            return $query->result();
    }

    public function updateApiStatusCall($user_id,$status){
        $sql    = "UPDATE {$this->table_name} SET status = 0 WHERE owner = {$user_id} AND status = {$status}";
        $this->db->query($sql);
        if($this->db->affected_rows()==0){
            die($this->lang->line("contactAdmin"));
        }
        return $this->db->affected_rows();
    }

    public function updateStatus($params){
            $sql    = "UPDATE {$this->table_name} SET status = {$params['status']}, owner = {$params['owner']} WHERE customerID = '".$params['customerID']."'";
            $this->db->query($sql);
            if($this->db->affected_rows()==0){
                die($this->lang->line("contactAdmin"));
            }
            return $this->db->affected_rows();
    }

    public function init_data($right){
		return array(
                               array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' => 'name',
						'label' => $this->lang->line('name'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
				array(
						'name' => 'phone',
						'label' => $this->lang->line('phone'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'airline',
						'label' => $this->lang->line('airline'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'ngay_dau_bay',
						'label' => $this->lang->line('ngay_dau_bay'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'caller',
						'label' => $this->lang->line('caller'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'status',
						'label' => $this->lang->line('status'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' 	=> 'button',
						'label' => $this->lang->line('button'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("tbl.*,tbl.owner AS saler,bh.owner,usr.username")
		->from($this->table_name.' AS tbl')
                ->join('book_history AS bh','bh.booking_place_code = tbl.booking_place_code','INNER')
                ->join('users AS usr','usr.id = bh.owner','LEFT')
                ->where('tbl.ngay_dau_bay = DATE_ADD(CURDATE(), INTERVAL +1 DAY)')
                ->where('(tbl.phone_callstatus = 3 OR tbl.phone_callstatus = 0)')
                ->group_by('customerID');
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();

		foreach($datatables['aaData'] as $item){
                    $getAllShip =   $this->getAllShip(array('booking_place_code'=>$item['booking_place_code'],'airline'=>$item['airline']));
                    //$loadShip   = json_decode($this->loadShip(array('booking_place_code'=>$item['booking_place_code'],'airline'=>$item['airline'])));

                    $json  = '';$ship = '';
                    
                    foreach($getAllShip as $keys => $vals){
                        $json   =   json_decode($vals->booking);
                        $ship   .=  'Hành trình ban ??u:<br/>'.trim($json[0]).'<br/>';
                    }
                   
                        switch ($item['phone_callstatus']){
                            case '3':
                                $phone_callstatus = $this->lang->line("phone_callback");
                                break;
                            case '4':
                                $phone_callstatus = $this->lang->line("callok");
                                break;
                            default :
                                $phone_callstatus = '';
                        }
                        $phone = json_decode($item['phone']);
			$ouput['aaData'][] = array(
                            $item['id'],
                            $item['name'],
                            $phone[0],
                            $item['booking_place_code'],
                            $item['airline'],
                            date("d-m-Y",strtotime($item['ngay_dau_bay'])),
                            ($item['status']==1)? $this->flyTomorrow_model->getInfoUserByParams(array('key'=>'id','value'=>$item['saler']))->username.'<br/>'.$this->lang->line('calling'):'---',
                            $phone_callstatus,
                            //$ship,//.'<br/>Hành trình t? load:<br/>'.$loadShip,
                            $item['username'],
                            $this->add_button_public($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}
        
}